public class Solution2 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        if (headA == null || headB == null) {

            return null;

        }

        ListNode curA = headA;

        ListNode curB = headB;

        int len1 = 0, len2 = 0;

        while (curA != null) {

            curA = curA.next;

            len1++;

        }

        while (curB != null) {

            curB = curB.next;

            len2++;

        }

        curA = headA;

        curB = headB;

        if (len1 > len2) {

            int a = len1 - len2;

            while (a != 0) {

                curA = curA.next;

                a--;

            }

        }

        if (len1 < len2) {

            int a = len2 - len1;

            while (a != 0) {

                curB = curB.next;

                a--;

            }

        }

        while (curA != null && curB != null) {

            if (curA == curB) return curA;

            curA = curA.next;

            curB = curB.next;

        }

        return null;

    }
}